/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.performance;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * Description: 为指标查询/趋势图 生成查询用的日期条件 其它地方如有用到类似查询条件的也可以使用此类
 * </p>
 * 
 * @author wangyazhou
 * @version 1.0
 * 
 * <p>
 * History:
 * 
 * Date Author Version Description
 * ---------------------------------------------------------------------------------
 * 2012-7-26 下午9:20:34 wangyazhou 1.0 To create
 * </p>
 * 
 * @since
 * @see
 */
public class DateGeneric {

    private static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //
    public static String today = "0";
    public static String yestoday = "1";
    public static String week = "2";
    public static String month = "3";
    public static String custom = "4";

    // 最近一天
    public static String lastOneDay = "10";
    // 最近三天
    public static String lastThrDay = "11";
    // 最近一周
    public static String lastOneWeek = "12";
    // 最近一月
    public static String lastOneMonth = "13";
    // 最近三个月
    public static String lastThrMonth = "14";
    // 最近半年
    public static String lastSixMonth = "15";
    // 最近一年
    public static String lastOneYear = "16";

    /**
     * 生成开始日期
     * 
     * @param timeSection
     * @param start
     * @return
     */
    public static String genericStartDate(String timeSection, String start) {
        Calendar now = Calendar.getInstance();
        if ("0".equals(timeSection)) { // 当天
            return sf.format(now.getTime()) + " 00:00:00";

        } else if ("1".equals(timeSection)) { // 前一天
            now.add(Calendar.DATE, -1);
            return sf.format(now.getTime()) + " 00:00:00";

        } else if ("2".equals(timeSection)) { // 本周
            int day_of_week = now.get(Calendar.DAY_OF_WEEK) - 1;
            if (day_of_week == 0) {
                day_of_week = 7;
            }
            now.add(Calendar.DATE, -day_of_week + 1);
            return sf.format(now.getTime()) + " 00:00:00";
        } else if ("3".equals(timeSection)) { // 本月
            now.set(Calendar.DAY_OF_MONTH, now.getActualMinimum(Calendar.DAY_OF_MONTH));
            return sf.format(now.getTime()) + " 00:00:00";
        } else if ("5".equals(timeSection)) { // 最近三月
            now.add(Calendar.MONTH, -3);
            now.set(Calendar.DAY_OF_MONTH, now.getActualMinimum(Calendar.DAY_OF_MONTH));
            return sf.format(now.getTime()) + " 00:00:00";
        } else if ("7".equals(timeSection)) { // 最近7天
            now.add(Calendar.DATE, -7);
            return sf.format(now.getTime()) + " 00:00:00";
        } else if ("8".equals(timeSection)) { // 最近一个月
            now.add(Calendar.MONTH, -1);
            now.set(Calendar.DAY_OF_MONTH, now.getActualMinimum(Calendar.DAY_OF_MONTH));
            return sf.format(now.getTime()) + " 00:00:00";
        } else if ("9".equals(timeSection)) { // 最近24小时
            now.add(Calendar.HOUR, -24);
            return sf.format(now.getTime()) + " 00:00:00";
        } else if ("4".equals(timeSection) && start != null && start.length() > 0) {

            return start;
        }
        return "";
    }

    /**
     * 生成结束日期
     * 
     * @param timeSection
     * @param end
     * @return
     */
    public static String getnericEndDate(String dim, String timeSection, String end) {
        Calendar now = Calendar.getInstance();
        if ("1".equals(timeSection)) { // 前一天
            now.add(Calendar.DATE, -1);
            return sf.format(now.getTime()) + " 23:59:59";
        } else if ("4".equals(timeSection) && end != null && end.length() > 0) {
            if ("0".equals(dim)) {
                return end + ":59:59";
            } else {
                return end + " 23:59:59";
            }
        } else { // 表示当前时间
            return "";
        }
    }

    /**
     * 为趋势图准备日期数据
     * 
     * @param timeSection
     * @param end
     * @return
     */
    public static String getnericEndDate2(String dim, String timeSection, String end) {
        Calendar now = Calendar.getInstance();
        if ("1".equals(timeSection)) { // 前一天
            now.add(Calendar.DATE, -1);
            return sf.format(now.getTime()) + " 23:59:59";
        } else if ("4".equals(timeSection) && end != null && end.length() > 0) {
            return end;
        } else { // 表示当前时间
            return sf2.format(now.getTime());
        }
    }

    /**
     * 得到本月的第一天
     * 
     * @return
     */
    public static String getMonthFirstDay(Calendar calendar, int month) {
        calendar.add(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

        return sf.format(calendar.getTime()) + " 00:00:00";
    }

    /**
     * 得到本月的最后一天
     * 
     * @return
     */
    public static String getMonthLastDay(Calendar calendar, int month) {
        calendar.add(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sf.format(calendar.getTime()) + " 23:59:59";
    }

    /**
     * 将一个日期字符串转化成Calendar
     * 
     * @param sDate
     *            String
     * @return Calendar
     */
    public static Calendar switchStringToCalendar(String sDate) {
        Date date = switchStringToDate(sDate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c;
    }

    /**
     * * 将一个日期字符串转化成日期 *
     * 
     * @param sDate
     *            String *
     * @return Date yyyy-mm-dd
     */
    public static Date switchStringToDate(String sDate) {
        Date date = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            date = df.parse(sDate);
        } catch (Exception e) {
            System.out.println("日期转换失败:" + e.getMessage());
        }
        return date;
    }

    /**
     * 取得指定月份的第一天
     *
     * @param strdate String
     * @return String
     */
    public static String getMonthBegin(String strdate) {
        java.util.Date date = switchStringToDate(strdate);
        return formatDateByFormat(date, "yyyy-MM") + "-01";
    }

    /**
     * 取得指定月份的最后一天
     *
     * @param strdate String
     * @return String
     */
    public static String getMonthEnd(String strdate) {
        java.util.Date date = switchStringToDate(getMonthBegin(strdate));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return formatDate(calendar.getTime());
    }

    /**
     * 常用的格式化日期
     *
     * @param date Date
     * @return String
     */
    public static String formatDate(java.util.Date date) {
        return formatDateByFormat(date, "yyyy-MM-dd");
    }

    /**
     * 以指定的格式来格式化日期
     *
     * @param date Date
     * @param format String
     * @return String
     */
    public static String formatDateByFormat(java.util.Date date, String format) {
        String result = "";
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static void main(String[] args) throws ParseException {
        DateGeneric dateg = new DateGeneric();
        System.out.println(getMonthBegin("2013-06-24"));
        System.out.println(getMonthEnd("2013-06-24"));

    }

}
